//
//  NSString+Regex.m
//  u9_iOS
//
//  Created by zhangzhenyun on 2017/8/21.
//  Copyright © 2017年 user1. All rights reserved.
//

#import "NSString+Regex.h"
#import <CommonCrypto/CommonDigest.h>

@implementation NSString (Regex)
- (BOOL)isMobile {
    NSString * mobile = @"^1(3[0-9]|4[0-9]|5[0-35-9]|8[025-9])\\d{8}$";
    NSString *  CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";
    NSString *  CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";
    NSString *  CT = @"^1((33|53|8[09])[0-9]|349)\\d{7}$";
    NSString *  AL = @"^17\\d{9}$";
    NSString *  L18 = @"^18\\d{9}$";
    NSString *  L15 = @"^15\\d{9}$";
    NSString *  L13 = @"^13\\d{9}$";
    if (self.isRule == NO) {
        return NO;
    }
    NSPredicate * regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", mobile];
    if ([regextestmobile evaluateWithObject:self]) {
        return YES;
    }
    
    NSPredicate * regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
    if ([regextestcm evaluateWithObject:self]) {
        return YES;
    }
    NSPredicate * regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
    if ([regextestcu evaluateWithObject:self]) {
        return YES;
    }
    NSPredicate * regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
    if ([regextestct evaluateWithObject:self]) {
        return YES;
    }
    
    NSPredicate * regextestal = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", AL];
    if ([regextestal evaluateWithObject:self]) {
        return YES;
    }
    NSPredicate * regextestL18 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", L18];
    if ([regextestL18 evaluateWithObject:self]) {
        return YES;
    }
    NSPredicate * regextestL15 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", L15];
    if ([regextestL15 evaluateWithObject:self]) {
        return YES;
    }
    NSPredicate * regextestL13 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", L13];
    if ([regextestL13 evaluateWithObject:self]) {
        return YES;
    }
    return NO;
}
- (BOOL)isRule {
    NSString * rule = @"[a-zA-Z0-9]*";
    NSPredicate * regex = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", rule];
    return [regex evaluateWithObject:self];
}
//- (BOOL)isPassWord {
//    return self.length >= 8 && self.length <= 16 && [self rangeOfString:@" "].length == 0 && self.isRule;
//}
- (BOOL)isPassWord {
    BOOL result = false;
    if ([self length] >= 8){
        // 判断长度大于8位后再接着判断是否同时包含数字和字符
        NSString * regex = @"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$";
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
        result = [pred evaluateWithObject:self];
    }
    return result;
}
- (NSString *)md5String {
    const char *cStr = [self UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5( cStr, (CC_LONG)strlen(cStr), result);
    NSString *md5Str = [NSString stringWithFormat:
                        @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
                        result[0], result[1], result[2], result[3], result[4],
                        result[5], result[6], result[7], result[8], result[9],
                        result[10], result[11],result[12], result[13], result[14], result[15]
                        ];
    return md5Str;
}
@end
